package com.gopay.cashier.service.impl;

import com.gopay.cashier.common.exception.WebException;
import com.gopay.cashier.service.TransLimitCheckService;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.rcs.TranDirection;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.acps.model.AcctMastBean;
import com.gopay.common.domain.rcs.req.RcsTransLimitRequest;
import com.gopay.common.domain.rcs.res.RcsTransLimitResponse;
import com.gopay.common.domain.solution.SolutionRequest;
import com.gopay.common.domain.solution.SolutionResponse;
import com.gopay.common.domain.solution.TranLimitReq;
import com.gopay.common.domain.solution.TranLimitRes;
import com.gopay.common.rcs.service.translimit.RcsTransLimitService;
import com.gopay.common.rcs.vo.RcsTransLimitView;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.acps.service.AcctTransRemoteService;
import com.gopay.remote.solution.SolutionRemoteService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 14-6-3.
 */

@SuppressWarnings("SpringJavaAutowiringInspection")
@Service(value = "transLimitCheckService")
class TransLimitCheckServiceImpl implements TransLimitCheckService {

    private Logger logger = LoggerFactory.getLogger(TransLimitCheckService.class);

    @Autowired
    protected AcctTransRemoteService acctTransService;

    @Resource(name = "rcsTransLimitService")
    private RcsTransLimitService rcsTransLimitService;

    @Resource(name = "transLimitConfService")
    protected SolutionRemoteService transLimitConfService;

    @Override
    public void checkAmtLimit(String acctId, TranDirection dir, IntTxnCd intTxnCd, BigDecimal tranAmt) throws WebException {
        logger.info("checkAmtLimit:acctId="+acctId+",tranDirection="+dir.name()+",intTxnCd="+intTxnCd.value+",tranAmt="+tranAmt.toString());
        if(StringUtils.isBlank(acctId) || dir==null || intTxnCd ==null || tranAmt==null)
            return ;

        RcsTransLimitView view = getTranLimitAmt(acctId, dir, intTxnCd);
        if(view!=null){
            List<RcsTransLimitRequest> requestList = getRcsTransLimitRequestList(view,tranAmt);
            for(RcsTransLimitRequest request : requestList){
                RcsTransLimitResponse resp = rcsTransLimitService.rcsTransLimitProcess(request);
                if(!org.apache.commons.lang.StringUtils.equals(resp.getProcCode(), ProcCodeConstants.PROC_CODE_200S1000)){
                    throw new WebException(resp.getProcCode());
                }
            }
        }
    }


    /**
     * 风控限额
     * @param acctId
     * @param dir
     * @param intTxnCd
     * @return
     */
    private RcsTransLimitView getTranLimitAmt(String acctId,TranDirection dir,IntTxnCd intTxnCd){
        TranLimitReq req = new TranLimitReq();
        req.setAcctType(acctId.substring(8, 10));
        req.setIntTxnCd(intTxnCd);
        req.setTranDirection(dir);
        AcctMastBean acctMastBean = acctTransService.getAccount(acctId);
        req.setCustId(acctMastBean.getCustId());
        req.setAcctProd(acctMastBean.getProdCode());
        SolutionRequest request = new SolutionRequest();
        request.setAcctId(acctId);
        request.setSequence(System.currentTimeMillis());
        request.setTimestamp(DateUtils.getSystemDate());
        request.setRequest(req);
        SolutionResponse res = transLimitConfService.resolve(request);
        if (null == res || false == res.isSucceed()) {
            return null;
        }
        TranLimitRes tres =  (TranLimitRes) res.getResponse();
        RcsTransLimitView view = new RcsTransLimitView();
        view.setAcctId(acctId);
        view.setAcctProd(req.getAcctProd());
        view.setCustId(req.getCustId());
        view.setIntTxnCd(intTxnCd);
        view.setTranDirection(dir);
        view.setTranLimitRes(tres);
        return  view;
    }

    private List<RcsTransLimitRequest> getRcsTransLimitRequestList(RcsTransLimitView view,BigDecimal tranAmt){
        List<RcsTransLimitRequest> requestList = new ArrayList<RcsTransLimitRequest>();
        RcsTransLimitRequest request = new RcsTransLimitRequest();
        request.setCustTranLimitConf(view.getTranLimitRes());
        request.setAcctProd(view.getAcctProd());
        request.setCustId(view.getCustId());
        request.setIntTxnCd(view.getIntTxnCd());
        request.setTranAmts(tranAmt);
        request.setTransDate(DateUtils.getCurrDate("yyyyMMdd"));
        request.setTranDirection(view.getTranDirection());
        request.setRequestType(RcsTransLimitRequest.RequestType.check);
        requestList.add(request);

//        RcsTransLimitRequest request2 = new RcsTransLimitRequest();
//        BeanUtils.copyProperties(request, request2);
//        request2.setRequestType(RcsTransLimitRequest.RequestType.add);
//        requestList.add(request2);

        return requestList;
    }
}
